<template>
  <div class="app-container">
    <el-form
      ref="form"
      :model="AutoGlobalAddFrom"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="参数名:" prop="parameName">
        <el-input v-model="AutoGlobalAddFrom.parameName" />
      </el-form-item>
      <el-form-item label="参数类型:" prop="parameType">
        <el-select
          v-model="AutoGlobalAddFrom.parameType"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in paramtype"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否必填:" prop="isRequired">
        <el-select
          v-model="AutoGlobalAddFrom.isRequired"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in isRequiredTest"
            :key="item.key"
            :label="item.key"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="默认值:" prop="defaultValue">
        <el-input
          v-model="AutoGlobalAddFrom.defaultValue"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否启用:" prop="globalState">
        <el-select
          v-model="AutoGlobalAddFrom.globalState"
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in globalStateList"
            :key="item.key"
            :label="item.key"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注:">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 50 }"
          placeholder="请输入内容"
          v-model="AutoGlobalAddFrom.remark"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{
          messageView.sumit
        }}</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
  
  <script>
import {
  getFindType,
  autoGlobalAdd,
  autoGlobalGet,
  autoGlobalPut
} from "@/api/autoGlobal";
export default {
  name: "autoGlobalFrom",
  data() {
    return {
      isAdd: true,
      isAddMessage: { sumit: "新增" },
      noAddMessage: { sumit: "编辑" },
      messageView: {},
      globalId:"",
      datasourceId: "",
      AutoGlobalAddFrom: {},
      parameName: "",
      paramtype: "",
      isRequired: "",
      defaultValue: "",
      globalState: "",
      rules: {
        parameName: [
          { required: true, message: "请输入参数名称", trigger: "blur" },
        ],
        paramtype: [
          { required: true, message: "请选择参数类型", trigger: "change" },
        ],
        isRequired: [
          { required: true, message: "请选择是否必填", trigger: "change" },
        ],
        defaultValue: [
          { required: true, message: "请输入默认值", trigger: "blur" },
        ],
        globalState: [
          { required: true, message: "请选择是否启用", trigger: "change" },
        ],
      },
      isRequiredTest: [
        { key: "必传", value: 0 },
        { key: "非必传", value: 1 },
      ],
      globalStateList: [
        { key: "启用", value: 0 },
        { key: "未启用", value: 1 },
      ],
    };
  },
  methods: {
    //查询请求类型
    findType() {
      getFindType().then((resp) => {
        this.paramtype = resp.data;
      });
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            autoGlobalAdd(this.AutoGlobalAddFrom).then((resp) => {
              this.$message({
                message: resp.msg,
                type: "success",
              });
              this.$router.push("/environment/autoGlobal");
            });
          } else {
            this.updateTrue();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    onCancel() {
      this.AutoGlobalAddFrom = {};
      this.$router.push("/environment/autoGlobal");
    },
    //查看编辑页面
    update(globalId) {
      autoGlobalGet(globalId).then((resp) => {
        this.AutoGlobalAddFrom = resp.data;
      });
    },
    //发送uodate请求
    updateTrue() {
      autoGlobalPut(this.AutoGlobalAddFrom).then((resp) => {
        this.$message({
          message: resp.msg,
          type: "success",
        });
        this.$router.push("/environment/autoGlobal");
      });
    },
  },
  created() {
    this.$router.onReady(() => {
      this.isAdd = this.$route.query.isAdd;
      this.globalId = this.$route.query.globalId;
    });
    this.findType();
    if (this.isAdd) {
      this.messageView = this.isAddMessage;
    } else {
      this.messageView = this.noAddMessage;
      this.update(this.globalId);
    }
  },
};
</script>
  
  <style scoped>
.line {
  text-align: center;
}
</style>
  
  